<template>
    <div class="goodsinfo-container">
       
        <transition
        @before-enter="beforeEnter"
        @enter="enter"
        @after-enter="afterEnter">
           <div class="ball" v-show="ballflag" ref="ball"></div>
        </transition>
        

        <!-- 商品轮播图区域 -->
        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
					</div>
				</div>
		</div>

        <!-- 商品 -->
        <div class="mui-card">
				<div class="mui-card-header">{{goodsinfo.title}}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p class="price">
                            市场价：<del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价:<span class="now_price">{{goodsinfo.sell_price}}</span>
                        </p>
                        <p>购买数量：<numbox @getcount="getSelectedCount" :max="goodsinfo.stock_quantity"></numbox></p>
                        <p>
                            <mt-button type="primary" size="small">立即购买</mt-button>
                            <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>

                        <!-- 分析：如何实现加入购物车的时候，拿到选择的数量 -->
                        <!-- 涉及到了子组件向父组件传值 -->
                        <!-- 事件调用的本质：父向子传递方法，子调用这个方法，同时把数据当做参数传递给这个方法 -->
                        </p>
					</div>
				</div>
		</div>
        <!-- 参数区域 -->
        <div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号：{{ goodsinfo.goods_no }}</p>
                        <p>库存情况：{{ goodsinfo.stock_quantity }}</p>
                        <p>上架时间：{{ goodsinfo.add_time | dateFormat}}</p>
					</div>
				</div>
				<div class="mui-card-footer">
                    <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                    <mt-button type="danger" size="large" plain @click="goComment(id)">图文评论</mt-button>
                </div>
		</div>
    </div>
</template>

<script>
import swiper from '../subcomponents/swiper'
import numbox from '../subcomponents/goodsinfo_numbox'

export default {
    data(){
        return{
            id:this.$route.params.id,    //将路由参数对象中的id挂载到data，方便后期调用
            lunbotu:[],
            goodsinfo:{},   //获取到的商品信息
            ballflag:false,   //控制小球隐藏与显示
            selectedCount:1
       }
    },
    created(){
        this.getLunbotu(),
        this.getGoodsInfo()
    },
    methods:{
        getLunbotu(){
            this.axios.get('../../../static/api/goodslunbotu.json').then((res) => {
                res.data.forEach(item => {
                    if(item.id==this.id){
                        this.lunbotu=this.lunbotu.concat(item)
                    }
                });
            })
        },
        getGoodsInfo(){
            this.axios.get('../../../static/api/goodsinfo.json').then((res) => {
                res.data.forEach(item => {
                    if(item.id==this.id){
                        this.goodsinfo=item                  
                    }
                });
            })
        },
        goDesc(id){
            //使用编程式导航跳转到图文介绍页面
          this.$router.push({ name: "goodsdesc", params: {id} })
        },
        goComment(id){
            //点击跳转评论页面
          this.$router.push({ name: "goodscomment", params: {id} })
        },
        addToShopCar(){
            // 添加到购物车
            this.ballflag =! this.ballflag;
            //拼接出一个要保存到store中car数组里的商品信息对象
            var goodsinfo = {
                id: this.id,
                count:this.selectedCount,
                price:this.goodsinfo.sell_price,
                selected:true
            };
            //调用store中的mutations来将商品加入购物车
            this.$store.commit("addToCar",goodsinfo)

        },
        beforeEnter(el){
            el.style.transform = "translate(0,0)";
        },
        enter(el,done){
            el.offsetWidth;

        //小球动画优化思路：
        //1.分辨率和测试时不一致，或者滚动条滚动，动画出现问题
        //2.分析：不能把位置的 横纵坐标 写死，应该根据不同情况，计算这个坐标值
        //3.解决：先得到徽标的横纵坐标，再得到小球的横纵坐标，就能计算出小球的相对位移

      //获取小球在页面中的位置
            const ballposition = this.$refs.ball.getBoundingClientRect();
      //获取徽标在页面中的位置
            const badgepositon = document.getElementById('badge').getBoundingClientRect();

            const xDist = badgepositon.left - ballposition.left;
            const yDist = badgepositon.top - ballposition.top;

            el.style.transform = `translate(${xDist}px, ${yDist}px)`;
            el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
            done()
        },
        afterEnter(el){
            this.ballflag =! this.ballflag;
        },
        getSelectedCount(count){
            // 当子组件把选中的数量传递给父组件的时候，把选中的值保存到data上
            this.selectedCount = count;
            // console.log('父组件拿到的数量值为'+this.selectedCount)
        }
    },

    components:{
        swiper,
        numbox
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-container{
    background-color: #eee;
    overflow: hidden;

    .now_price{
        color: red;
        font-size:16px;
        font-weight:bold;
    }
}

.mui-card-footer{
    display: block;
    button{
        margin: 15px 0;
    }
}

.ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 390px;
    left: 148px;
}
</style>